<?php

namespace App\Containers\AppSection\LegalForm\UI\API\Transformers;

use App\Containers\AppSection\Data\UI\API\Transformers\CompanyTransformer;
use App\Containers\AppSection\Data\UI\API\Transformers\PassportTransformer;
use App\Containers\AppSection\Data\UI\API\Transformers\PersonalTransformer;
use App\Containers\AppSection\File\UI\API\Transformers\FileTransformer;
use App\Containers\AppSection\LegalForm\Models\LegalForm;
use App\Containers\AppSection\Profile\UI\API\Transformers\ProfileTransformer;
use App\Containers\AppSection\Questionnaire\UI\API\Transformers\QuestionnaireTransformer;
use App\Ship\Parents\Transformers\Transformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;

class LegalFormTransformer extends Transformer
{
    protected array $defaultIncludes = [
        'personal', 'passport', 'company', 'documents'
    ];

    /**
     * @var  array
     */
    protected array $availableIncludes = [
        'questionnaire', 'profile'
    ];

    public function transform(LegalForm $t): array
    {
        return [
            'object' => $t->getResourceKey(),
            'include' => $this->getAvailableIncludes(),
            'id' => $t->getHashedKey(),
            'created_at' => $this->dateFormatter($t->created_at),
            'updated_at' => $this->dateFormatter($t->updated_at),
            'readable_created_at' => $t->created_at->diffForHumans(),
            'readable_updated_at' => $t->updated_at->diffForHumans()];
    }

    public function includePersonal(LegalForm $t): Item
    {
        return $this->item($t->personal, new PersonalTransformer);
    }

    public function includePassport(LegalForm $t): Item
    {
        return $this->item($t->passport, new PassportTransformer);
    }

    public function includeQuestionnaire(LegalForm $t): Item
    {
        return $this->item($t->questionnaire, new QuestionnaireTransformer);
    }

    public function includeCompany(LegalForm $t): ?Item
    {
        return $this->nullableItem($t->company, new CompanyTransformer);
    }

    public function includeDocuments(LegalForm $t): Collection
    {
        return $this->collection($t->documents()->get(), new FileTransformer);
    }

    public function includeProfile(LegalForm $t): ?Item
    {
        return $this->nullableItem($t->profile, new ProfileTransformer);
    }
}
